package org.mch.func.db.pdmfile;

import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.DefaultListModel;

import org.mch.biz.ui.FuncEvent;
import org.mch.biz.ui.FuncPanel;

import com.mch.main.common.MEnv;

public class PDMPanelEvent extends FuncEvent {
	private static final long serialVersionUID = 8238425469235837527L;

	public PDMPanelEvent(FuncPanel panel) {
		super(panel);
	}

	@Override
	protected void onButtonClick(ActionEvent e) throws Exception {
		if (getPanel().getButton_loadpdm() == e.getSource()) {
			onLoadPdm();
		} else if (getPanel().getBtn_javabean() == e.getSource()) {
			onBuildJavaBean();
		} else if (getPanel().getButton_selectpdm() == e.getSource()) {
			onSelectedPDMFile();
		}
	}

	private void onSelectedPDMFile() {
		getPanel().getSelectPdmFile().showOpenDialog(getPanel());
		File selPdmFile = getPanel().getSelectPdmFile().getSelectedFile();
		if (selPdmFile != null)
			getPanel().getTextField_pdm().setText(selPdmFile.getAbsolutePath());
	}

	private void onBuildJavaBean() throws Exception {
		int index = getPanel().getList_pdm().getSelectedIndex();
		if (index != -1) {
			PDMTableObject pdmto = (PDMTableObject) getPanel().getPdmTableList().get(index);

			String extendEO = getPanel().getTextExtendEO().getValue();

			String fieldtype = "public";
			if (getPanel().getRdbtnPublic().isSelected())
				fieldtype = "public";
			else if (getPanel().getRdbtnPrivate().isSelected())
				fieldtype = "private";
			else if (getPanel().getRdbtnProtected().isSelected()) {
				fieldtype = "protected";
			}

			boolean isnewts = false;
			if (getPanel().getRdbtn_newts().isSelected()) {
				isnewts = true;
			} else if (getPanel().getRdbtn_notnewts().isSelected()) {
				isnewts = false;
			}

			String rightText = null;
			String packageName = getPanel().getTextPackage().getText();
			String authorName = getPanel().getTextAuthor().getText();
			if (getPanel().getRdbtnJavabean().isSelected()) {
				rightText = PDMTools.buildJavaBean(pdmto, fieldtype, packageName, authorName, isnewts);
			} else if (getPanel().getRBtnJpaEntity().isSelected()) {
				// getPanel().getTextArea_right().setText(PDMTools.getNCSuperVO(pdmto).toString());
			} else if (getPanel().getRdbtnHibernatevo().isSelected()) {
				rightText = PDMTools.getHibernateVO(pdmto, fieldtype).toString();
			} else if (getPanel().getRBtnBaseEO().isSelected()) {
				rightText = PDMTools.getMCHBaseEO(pdmto, fieldtype, extendEO, isnewts).toString();
			} else if (getPanel().getRadioBtnEntityEO().isSelected()) {
				rightText = PDMTools.buildEntityEO(pdmto, fieldtype, packageName, authorName, isnewts);
			}

			getPanel().getTextArea_right().setText(rightText);
		} else {
			MEnv.setStatusMsg("请选择要生成的表名！");
		}
	}

	private void onLoadPdm() throws Exception {
		((DefaultListModel<String>) getPanel().getList_pdm().getModel()).removeAllElements();

		getPanel().setPdmTableList(PDMTools.getPdmTableObject(new File(getPanel().getTextField_pdm().getText())));

		if ((getPanel().getPdmTableList() != null) && (getPanel().getPdmTableList().size() > 0)) {
			for (int i = 0; i < getPanel().getPdmTableList().size(); i++) {
				String tableName = ((PDMTableObject) getPanel().getPdmTableList().get(i)).getTableName();
				((DefaultListModel<String>) getPanel().getList_pdm().getModel()).addElement(tableName);
			}
		}
	}

	@Override
	public PDMPanel getPanel() {
		return (PDMPanel) super.getPanel();
	}
}
